package com.meringer.beans;


import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.meringer.beans.ejb.UserEJB;
import com.meringer.entities.User;


@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean {
	@EJB
	private UserEJB userEJB;
	
	public User getCurrentUser()
	{
		User user = userEJB.currentUser();
		if(user.getImgPath() == null)
			user.setImgPath("img/avatars/no-avatar.png");
		return user;
	}	
	public int getUserLevel(){
		return getCurrentUser().getExperience()/100;
	}
	public int getUserXPToNextLevel(){
		return 100 -(getCurrentUser().getExperience()%100);
	}
	public int getUserXPForImg(){
		return getCurrentUser().getExperience()%100;
	}
	public String logout() {
		FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
	    return "index?faces-redirect=true";	    
	}
//	public void saveUser(User user){
//		this.user = user;
//	}
}
